/****************************************************************************
*
* Copyright (c) 2023  C*Core -   All Rights Reserved
*
* THIS SOFTWARE IS DISTRIBUTED "AS IS, " AND ALL WARRANTIES ARE DISCLAIMED,
* INCLUDING MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* PROJECT     : CCFC2011BC
* DESCRIPTION : CCFC2011BC iic low level driver code c file
* HISTORY     : Initial version.
* @file     iic_lld.c
* @version  1.1
* @date     2023 - 02 - 20
* @brief    Initial version.
*
*****************************************************************************/

#include "CCFC2011BC.h"
#include "console.h"
#include "IntcInterrupts.h"
#include "intc_lld.h"
#include "iic_lld.h"
#include "sys.h"
#include "edma_lld.h"

/* Table of base addresses for I2C instances. */
static I2C_Type * const g_i2cBase[1u] = I2C_BASE_PTRS;

/* Pointer to runtime state structure. */
static i2c_master_state_t* g_i2cMasterStatePtr = NULL;
static i2c_slave_state_t* g_i2cSlaveStatePtr = NULL;

/************************************************************************
 *    Functional declarations
 ************************************************************************/
static void I2C_Clear_BusInterruptFlag(I2C_Type *baseAddr, uint8_t enable);
static void I2C_Clear_MasterArbitrationLostFlag(I2C_Type *baseAddr);

static uint8_t I2C_Get_SlaveTransmitMode(const I2C_Type *baseAddr);
static uint8_t I2C_Get_ReceivedACK(const I2C_Type *baseAddr);
static uint8_t I2C_Get_AddressAsSlave(const I2C_Type *baseAddr);
static uint8_t I2C_Get_MasterArbitrationLostEvent(const I2C_Type *baseAddr);
static uint8_t I2C_Get_DataIBDR(const I2C_Type *baseAddr);
static uint8_t I2C_Get_BusBusyEvent(const I2C_Type *baseAddr);
static uint8_t I2C_Get_BusInterruptFlag(const I2C_Type *baseAddr);
static uint8_t I2C_Get_ModuleTransmitMode(const I2C_Type *baseAddr);
static uint8_t I2C_Get_TCF(const I2C_Type *baseAddr);
static uint8_t I2C_Get_ModuleModSelect(const I2C_Type *baseAddr);

static void I2C_Set_ModuleModSelect(I2C_Type *baseAddr, i2c_module_type moduleType);
static void I2C_Set_DataAcknowledge(I2C_Type *baseAddr, uint8_t enable);
static void I2C_Set_ModuleEnable(I2C_Type *baseAddr, uint8_t enable);
static void I2C_Set_RepeatStart(I2C_Type *baseAddr, uint8_t enable);
static void I2C_Set_ModuleTransmitMode(I2C_Type *baseAddr, uint8_t enable);
static void I2C_Set_DataIBDR(I2C_Type *baseAddr,  uint8_t data);
static void I2C_Set_BusIRQEnable(I2C_Type *baseAddr, uint8_t enable);
static void I2C_Set_BusIdleIRQ(I2C_Type *baseAddr, uint8_t enable);
static void I2C_Set_SlaveAddress(I2C_Type *baseAddr, uint8_t data);
static void I2C_Set_DMAEnable(I2C_Type *baseAddr, uint8_t enable);

static void I2C_ResetModuleState(I2C_Type *baseAddr);

static uint8_t I2C_LLD_MasterTransmitAddress(uint8_t instance);
static void I2C_LLD_MasterSendDataEvent(I2C_Type *baseAddr, i2c_master_state_t *master);
static void I2C_LLD_MasterReceiveAckEvent(uint32_t instance);
static void I2C_LLD_GenerateStopSignal(uint8_t instance);
static void I2C_LLD_MasterEndTransfer(uint8_t instance, i2c_master_state_t *master);
static void I2C_LLD_MasterGenerateStopEvent(uint8_t instance);
static void I2C_LLD_MasterLastByteTransmitted(uint8_t instance);
static void I2C_LLD_MasterTransmitHandler(uint8_t instance);
static void I2C_LLD_MasterReceiveHandler(uint8_t instance);
static void I2C_LLD_MasterIRQHandler(uint8_t instance);
static void I2C_LLD_MasterArbitrationLostHandler(uint8_t instance);
static void I2C_LLD_MasterToSlaveHandler(uint8_t instance);
static void I2C_LLD_SlaveStopEvent(I2C_Type *baseAddr, i2c_slave_state_t *slave);
static void I2C_LLD_SlaveSendDataEvent(I2C_Type *baseAddr, i2c_slave_state_t *slave);
static void I2C_LLD_AddressAsSlaveEvent(I2C_Type *baseAddr);
static void I2C_LLD_SlaveReceiveDataEvent(uint8_t instance, i2c_slave_state_t *slave);
static void I2C_LLD_SlaveEndTransfer(I2C_Type *baseAddr, i2c_slave_state_t *slave);
static void I2C_LLD_SlaveTransmitDataEvent(uint32_t instance);
static void I2C_LLD_SlaveTransferHandler(uint8_t instance);
static void I2C_LLD_SlaveHandleTransferEvents(uint8_t instance);
static void I2C_LLD_BusBusyEventHandler(uint8_t instance);
static void I2C_LLD_SlaveArbitrationLostHandler(uint8_t instance);
static void I2C_LLD_SlaveIRQHandler(uint8_t instance);
static void I2C_LLD_MasterReceiveDataEvent(uint8_t instance, i2c_master_state_t *master);
static void I2C_Init_IrqIsrEnable(uint8_t instance);

static void I2C_LLD_IRQHandler(uint8_t instance);
static uint8_t I2C_LLD_Polling_Send(uint8_t data, I2C_Type *baseAddr);
static void I2C_LLD_Polling_Stop(I2C_Type *baseAddr);

/************************************************************************
 *    Functional declarations
 ************************************************************************/

static void I2C_LLD_IRQHandler0(void)
{
    I2C_LLD_IRQHandler(0);
}

/*******************************************************************************
 * @brief      IIC SCL/SDA IO CONFIG.
 * @param[in]  uint8_t index, uint8_t mode
 * @param[out] None
 * @retval     None
 * @notapi      
 *******************************************************************************/
void I2C_LLD_Gpio_Init(uint8_t index, uint8_t mode)
{
    if ((uint8_t)I2C_0_index == index)
    {   /** i2c_0 **/
        if ((uint8_t)I2C_GPIO_DEFAULT == mode)
        {
            SIU.PSMI[29].R = 0;
            SIU.PSMI[30].R = 0;
            SIU.PCR[10].R = 0x0b23;        /** I2C0: PA[10] SDA, Enable pull - up resistor **/
            SIU.PCR[11].R = 0x0b23;        /** I2C0: PA[11] SCL, Enable pull - up resistor **/
        }
        else
        {
            SIU.PSMI[29].R = 0x01;
            SIU.PSMI[30].R = 0x01;
            SIU.PCR[18].R = 0x0b23;           /** I2C0: PB[2] SDA, Enable pull - up resistor **/
            SIU.PCR[19].R = 0x0b23;           /** I2C0: PB[3] SCL, Enable pull - up resistor **/
        }
    }
    else
    {
    }
}
/*******************************************************************************
 * @brief      IIC SCL/SDA IO CONFIG.
 * @param[in]  uint8_t index, uint8_t mode
 * @param[out] None
 * @retval     None
 * @notapi      
 *******************************************************************************/
void I2C_LLD_Gpio_DeInit(uint8_t index, uint8_t mode)
{
    if ((uint8_t)I2C_0_index == index)
    {   /** i2c_0 **/
        if ((uint8_t)I2C_GPIO_DEFAULT == mode)
        {
            SIU.PSMI[29].R = 0;
            SIU.PSMI[30].R = 0;
            SIU.PCR[10].R = 0x0023;        /** I2C0: PA[10] SDA, Enable pull - up resistor **/
            SIU.PCR[11].R = 0x0023;        /** I2C0: PA[11] SCL, Enable pull - up resistor **/
        }
        else
        {
            SIU.PSMI[29].R = 0x01;
            SIU.PSMI[30].R = 0x01;
            SIU.PCR[18].R = 0x0023;           /** I2C0: PB[2] SDA, Enable pull - up resistor **/
            SIU.PCR[19].R = 0x0023;           /** I2C0: PB[3] SCL, Enable pull - up resistor **/
        }
    }
    else
    {
    }
}

/*******************************************************************************
 * @brief      I2C_IBSR_IBIF_FUNC
 * @param[in]  uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_IBSR_IBIF_FUNC(uint8_t enable)
{
	uint8_t temp = enable;

	temp = temp << 1u;

	temp = (uint8_t)(temp & 0x2u);

	return temp;
}

/*******************************************************************************
 * @brief      This function shall be clear interrupt flag.
 * @param[in]  I2C_Type *baseAddr, uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi      
 *******************************************************************************/
static void I2C_Clear_BusInterruptFlag(I2C_Type *baseAddr, uint8_t enable)
{
    uint8_t regValue = baseAddr->IBSR;

    /* Clear I - Bus Interrupt Flag */
    regValue &= (uint8_t)((~(I2C_IBSR_IBIF_MASK)) & (~(0x02u))); /* PRQA S 2905 */

    regValue |= I2C_IBSR_IBIF_FUNC(enable);

    baseAddr->IBSR = (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      I2C_IBSR_IBAL_FUNC
 * @param[in]  uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_IBSR_IBAL_FUNC(uint8_t enable)
{
	uint8_t temp = enable;

	temp = temp << 4u;

	temp = (uint8_t)(temp & 0x10u);

	return temp;
}

/*******************************************************************************
 * @brief      This function clears the arbitration lost flag.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_Clear_MasterArbitrationLostFlag(I2C_Type *baseAddr)
{
    uint8_t enable = 1;

    uint8_t regValue = (uint8_t)baseAddr->IBSR;

    regValue &= (uint8_t)((~(0x10u)) & (~(0x2u))); /* PRQA S 2905 */

    regValue |= I2C_IBSR_IBAL_FUNC(enable);

    baseAddr->IBSR = (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      Check if the slave is transmit or receive mode.
 *             This function returns true if the I2C is in transmitting mode
 *             and false if the slave is in receive mode.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     indication that slave is in transmitting mode
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_Get_SlaveTransmitMode(const I2C_Type *baseAddr)
{
    uint8_t regValue = (uint8_t) baseAddr->IBSR;

    regValue = (regValue & I2C_IBSR_SRW_MASK) >> I2C_IBSR_SRW_SHIFT;

    return (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      This function shall be get ACK Flag.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     Received Acknowledge.
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_Get_ReceivedACK(const I2C_Type *baseAddr)
{
    uint8_t regValue = (uint8_t) (baseAddr->IBSR);

    regValue = (regValue & I2C_IBSR_RXAK_MASK);

    if (regValue == 0u)
    {
    	regValue = 1u;
    }
    else
    {
        regValue = 0u;
    }

    return regValue;
}

/*******************************************************************************
 * @brief      This function shall be get address as slaver flag.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     slave or master.
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_Get_AddressAsSlave(const I2C_Type *baseAddr)
{
    uint8_t regValue = (uint8_t) baseAddr->IBSR;

    regValue = (regValue & I2C_IBSR_IAAS_MASK) >> I2C_IBSR_IAAS_SHIFT;

    return (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      This function shall be get master arbitration lost event.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     None
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_Get_MasterArbitrationLostEvent(const I2C_Type *baseAddr)
{
    uint8_t regValue = (uint8_t) baseAddr->IBSR;

    regValue = (regValue & 0x10u) >> 4u;

    return (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      This function shall be get data from I2C register.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     data
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_Get_DataIBDR(const I2C_Type *baseAddr)
{
    uint8_t regValue = (uint8_t) baseAddr->IBDR;

    return (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      This function shall be get bus busy event.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     idle or busy
 * @notapi     
 *******************************************************************************/
static uint8_t I2C_Get_BusBusyEvent(const I2C_Type *baseAddr)
{
    uint8_t regValue = (uint8_t) baseAddr->IBSR;

    regValue = (regValue & I2C_IBSR_IBB_MASK) >> I2C_IBSR_IBB_SHIFT;

    return (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      This function shall be get bus interrupt flag.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     None
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_Get_BusInterruptFlag(const I2C_Type *baseAddr)
{
    uint8_t regValue = (uint8_t) baseAddr->IBSR;

    regValue = (regValue & I2C_IBSR_IBIF_MASK) >> I2C_IBSR_IBIF_SHIFT;

    return (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      This function shall be get module transmit mode.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     Transmit or receive.
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_Get_ModuleTransmitMode(const I2C_Type *baseAddr)
{
    uint8_t regValue = (uint8_t) baseAddr->IBCR;

    regValue = (regValue & I2C_IBCR_TXRX_MASK) >> I2C_IBCR_TXRX_SHIFT;

    return  regValue;
}

/*******************************************************************************
 * @brief      This function shall be get TCF flag.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     complete or in progress.
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_Get_TCF(const I2C_Type *baseAddr)
{
    uint8_t regValue =  (uint8_t) baseAddr->IBSR;

    regValue = (regValue & I2C_IBSR_TCF_MASK) >> I2C_IBSR_TCF_SHIFT;

    return regValue;
}

/*******************************************************************************
 * @brief      This function shall be get module mode  .
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     Master mode or slave mode.
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_Get_ModuleModSelect(const I2C_Type *baseAddr)
{
    uint8_t regValue = (uint8_t) baseAddr->IBCR;

    regValue = (regValue & I2C_IBCR_MSSL_MASK) >> I2C_IBCR_MSSL_SHIFT;

    return (uint8_t)regValue;
}

/*******************************************************************************
 * @brief      I2C_IBCR_MSSL_FUNC
 * @param[in]  enable
 * @param[out] None
 * @retval     regvalue
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_IBCR_MSSL_FUNC(uint8_t enable)
{
	uint8_t temp = enable;

	temp = temp << 5u;

	temp = (uint8_t)(temp & 0x20u);

	return temp;
}

/*******************************************************************************
 * @brief      This function shall be set module mode.
 * @param[in]  I2C_Type *baseAddr, i2c_module_type moduleType
 * @param[out] None
 * @retval     None
 * @notapi      
 *******************************************************************************/
static void I2C_Set_ModuleModSelect(I2C_Type *baseAddr, i2c_module_type moduleType)
{
    uint8_t regValue = (uint8_t) baseAddr->IBCR;

    regValue &= (uint8_t) (~(I2C_IBCR_MSSL_MASK));

    regValue |= I2C_IBCR_MSSL_FUNC((uint8_t)moduleType);

    baseAddr->IBCR = (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      I2C_IBCR_NOACK_FUNC
 * @param[in]  enable
 * @param[out] None
 * @retval     Regvalue
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_IBCR_NOACK_FUNC(uint8_t enable)
{
     uint8_t temp = 0u;

     if (enable == 0u)
     {
      temp = 1u;
     }

     temp = temp << 3u;

     return temp;
}

/*******************************************************************************
 * @brief      This function shall be set data ACK.
 * @param[in]  I2C_Type *baseAddr, uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_Set_DataAcknowledge(I2C_Type *baseAddr, uint8_t enable)
{
    uint8_t regValue = (uint8_t) baseAddr->IBCR;

    regValue &= (uint8_t) (~(I2C_IBCR_NOACK_MASK));

    regValue |= I2C_IBCR_NOACK_FUNC(enable);

    baseAddr->IBCR = (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      I2C_IBCR_MDIS_FUNC
 * @param[in]  enable
 * @param[out] None
 * @retval     Regvalue
 * @notapi     
 *******************************************************************************/
static uint8_t I2C_IBCR_MDIS_FUNC(uint8_t enable)
{
     uint8_t temp = 0u;

     if (enable == 0u)
     {
      temp = 1u;
     }

     temp = temp << 7u;

     return temp;
}

/*******************************************************************************
 * @brief      This function shall be set Module Enable.
 * @param[in]  I2C_Type *baseAddr, uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_Set_ModuleEnable(I2C_Type *baseAddr, uint8_t enable)
{
    uint8_t regValue = (uint8_t) baseAddr->IBCR;

    regValue &= (uint8_t) (~(I2C_IBCR_MDIS_MASK));

    regValue |= I2C_IBCR_MDIS_FUNC(enable);

    baseAddr->IBCR  = (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      I2C_IBCR_RSTA_FUNC
 * @param[in]  enable
 * @param[out] None
 * @retval     Regvalue
 * @notapi     
 *******************************************************************************/
static uint8_t I2C_IBCR_RSTA_FUNC(uint8_t enable)
{
     uint8_t temp = enable;

     temp = temp << 2u;

     temp = (uint8_t)(temp & 0x4u);

     return temp;
}

/*******************************************************************************
 * @brief      This function shall be set repeat start.
 * @param[in]  I2C_Type *baseAddr, uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_Set_RepeatStart(I2C_Type *baseAddr, uint8_t enable)
{
    uint8_t regValue = (uint8_t) baseAddr->IBCR;

    regValue &= (uint8_t) (~(I2C_IBCR_RSTA_MASK));

    regValue |= I2C_IBCR_RSTA_FUNC(enable);

    baseAddr->IBCR  = (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      I2C_IBCR_TXRX_FUNC.
 * @param[in]  enable
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static uint8_t I2C_IBCR_TXRX_FUNC(uint8_t enable)
{
     uint8_t temp = enable;

     temp = temp << 4u;

     temp = (uint8_t)(temp & 0x10u);

     return temp;
}

/*******************************************************************************
 * @brief      This function shall be set module Transmit mode.
 * @param[in]  I2C_Type *baseAddr, uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_Set_ModuleTransmitMode(I2C_Type *baseAddr, uint8_t enable)
{
    uint8_t regValue = (uint8_t) baseAddr->IBCR;

    regValue &= (uint8_t) (~(I2C_IBCR_TXRX_MASK));

    regValue |= I2C_IBCR_TXRX_FUNC(enable);

    baseAddr->IBCR = (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      This function shall be set the Data register.
 * @param[in]  I2C_Type *baseAddr,  uint8_t data
 * @param[out] None
 * @retval     None
 * @notapi      
 *******************************************************************************/
static void I2C_Set_DataIBDR(I2C_Type *baseAddr,  uint8_t data)
{
    baseAddr->IBDR = (uint8_t) data;
}

/*******************************************************************************
 * @brief      I2C_IBCR_IBIE_FUNC
 * @param[in]  enable
 * @param[out] None
 * @retval     None
 * @notapi      
 *******************************************************************************/
static uint8_t I2C_IBCR_IBIE_FUNC(uint8_t enable)
{
     uint8_t temp = enable;

     temp = temp << 6u;

     temp = (uint8_t)(temp & 0x40u);

     return temp;
}

/*******************************************************************************
 * @brief      This function shall be set bus interrupt enable.
 * @param[in]  I2C_Type *baseAddr, uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_Set_BusIRQEnable(I2C_Type *baseAddr, uint8_t enable)
{
    uint8_t regValue = (uint8_t) baseAddr->IBCR;

    regValue &= (uint8_t) (~(I2C_IBCR_IBIE_MASK));

    regValue |= I2C_IBCR_IBIE_FUNC(enable);

    baseAddr->IBCR = (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      I2C_IBCR_BIIE_FUNC
 * @param[in]  uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static uint8_t I2C_IBCR_BIIE_FUNC(uint8_t enable)
{
     uint8_t temp = enable;

     temp = temp << 7u;

     return temp;
}
/*******************************************************************************
 * @brief      This function shall be set bus idle.
 * @param[in]  I2C_Type *baseAddr, uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_Set_BusIdleIRQ(I2C_Type *baseAddr, uint8_t enable)
{
    uint8_t regValue = (uint8_t) baseAddr->IBIC;

    regValue &= (uint8_t) (~(I2C_IBIC_BIIE_MASK));

    regValue |= I2C_IBCR_BIIE_FUNC(enable);

    baseAddr->IBIC = (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      I2C_IBAD_ADR_FUNC
 * @param[in]  uint8_t enable
 * @param[out] None
 * @retval     Regvalue
 * @notapi     
 *******************************************************************************/
static uint8_t I2C_IBAD_ADR_FUNC(uint8_t enable)
{
     uint8_t temp = enable;

     temp = temp << 1u;

     return temp;
}
/*******************************************************************************
 * @brief      This function shall be set the slave address.
 * @param[in]  I2C_Type *baseAddr, uint8_t data
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_Set_SlaveAddress(I2C_Type *baseAddr, uint8_t data)
{
    uint8_t regValue = (uint8_t) baseAddr->IBAD;

    regValue &= (uint8_t) (~(I2C_IBAD_ADR_MASK));

    regValue |= I2C_IBAD_ADR_FUNC(data);

    baseAddr->IBAD = (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      I2C_IBAD_ADR_FUNC
 * @param[in]  uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static uint8_t I2C_IBCR_DMAEN_FUNC(uint8_t enable)
{
     uint8_t temp = enable;

     temp = temp << 1u;

     temp = (uint8_t)(temp & 0x2u);

     return temp;
}
/*******************************************************************************
 * @brief      This funciton shall be set DMA enable bit.
 * @param[in]  I2C_Type *baseAddr, uint8_t enable
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_Set_DMAEnable(I2C_Type *baseAddr, uint8_t enable)
{
    uint8_t regValue = baseAddr->IBCR;

    regValue &= (uint8_t) (~(I2C_IBCR_DMAEN_MASK));

    regValue |= I2C_IBCR_DMAEN_FUNC(enable);

    baseAddr->IBCR = (uint8_t) regValue;
}

/*******************************************************************************
 * @brief      This function shall be reset the  module state.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_ResetModuleState(I2C_Type *baseAddr)
{
    /* Reset the I2C Bus Control Register values */
    baseAddr->IBCR = 0x80;
    /* Reset the I2C Bus Interrupt Configuration Register */
    baseAddr->IBIC = 0x00;
}

/*******************************************************************************
 * @brief      IIC Master Mode send address data.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static uint8_t I2C_LLD_MasterTransmitAddress(uint8_t instance)
{
    uint8_t address;

    uint8_t temp = 0u;
    I2C_Type *baseAddr;

    const i2c_master_state_t * master;
    baseAddr = g_i2cBase[instance];

    master = g_i2cMasterStatePtr;

    /* Select master mode */
    I2C_Set_ModuleModSelect(baseAddr, I2C_MASTER);

    /* Select transmit mode for master */
    I2C_Set_ModuleTransmitMode(baseAddr, 1);

    /* Transmit address = slave address + write/read flag */
    if (master->sendData == 0u)
    {
    temp = 1u;
    }
    else
    {
       temp = 0u;
    }

    address = (uint8_t)(master->slaveAddress << 1U) + temp;

    I2C_Set_DataIBDR(baseAddr, address);

    /* In case master want to receive one byte set NACK */
    if ((master->sendData == 0u) && (master->rxSize == 1u))
    {
        I2C_Set_DataAcknowledge(baseAddr, 0);                        /* Send NACK */
    }

    return 0;
}

/*******************************************************************************
 * @brief      This function shall be the master send data event.
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_LLD_MasterSendDataEvent(I2C_Type *baseAddr, i2c_master_state_t *master)
{
    if (master->txSize > 0U)
    {
        I2C_Set_DataIBDR(baseAddr, master->txBuff[0U]);
        master->txBuff++;
        master->txSize--;
    }
}

/*******************************************************************************
 * @brief      This function shall be the master receive ACK event.
 * @param[in]  uint32_t instance
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_LLD_MasterReceiveAckEvent(uint32_t instance)
{
    i2c_master_state_t * master;

    I2C_Type *baseAddr;
    baseAddr = g_i2cBase[instance];
    master = g_i2cMasterStatePtr;

    if (master->sendData == 1u)
    {
        /* Send the next byte of data */
        I2C_LLD_MasterSendDataEvent(baseAddr, master);
    }
    else
    {
        /* If is the end of address cycle and master is in RX mode */
         I2C_Set_ModuleTransmitMode(baseAddr, master->sendData);

        /* Dummy read from IBDR */
         (void) I2C_Get_DataIBDR(baseAddr);
    }
}

/*******************************************************************************
 * @brief      This function shall be generate stop signal,
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_LLD_GenerateStopSignal(uint8_t instance)
{
    i2c_master_state_t * master;
    I2C_Type *baseAddr;

    baseAddr = g_i2cBase[instance];
    master = g_i2cMasterStatePtr;

    /* Check if the module is in master mode */
    if (I2C_Get_ModuleModSelect(baseAddr) == 1u)
    {
        /* Generate stop */
        I2C_Set_ModuleModSelect(baseAddr, I2C_SLAVE);
        master->stopGenerated = 1;
    }
}

/*******************************************************************************
 * @brief      This function shall be end rhe master transfer.
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi  
 *******************************************************************************/
static void I2C_LLD_MasterEndTransfer(uint8_t instance, i2c_master_state_t *master)
{
    /* Disable all events */
    I2C_Type *baseAddr;
    baseAddr = g_i2cBase[instance];

    I2C_Set_BusIRQEnable(baseAddr, 0);
    I2C_Set_BusIdleIRQ(baseAddr, 0);

    master->txBuff = NULL;
    master->txSize = 0;
    master->rxBuff = NULL;
    master->rxSize = 0;
    master->i2cIdle = 1;
}

/*******************************************************************************
 * @brief      This function shall be the master generate stop event.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_LLD_MasterGenerateStopEvent(uint8_t instance)
{
    i2c_master_state_t * master;

    master = g_i2cMasterStatePtr;
    /* Generate Stop Signal */
    I2C_LLD_GenerateStopSignal(instance);

    /* Master End Transfer */
    I2C_LLD_MasterEndTransfer(instance, master);
}

/*******************************************************************************
 * @brief      This function shall be the master transmit the last byte.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_LLD_MasterLastByteTransmitted(uint8_t instance)
{
    i2c_master_state_t * master;
    I2C_Type *baseAddr;

    baseAddr = g_i2cBase[instance];
    master = g_i2cMasterStatePtr;

    if (master->sendStop == 1u)
    {
        /* Generate stop signal */
        I2C_LLD_GenerateStopSignal(instance);
    }
    else
    {
        /* Generate repeated start */
        I2C_Set_RepeatStart(baseAddr, 1);
        master->stopGenerated = 0;
        master->i2cIdle = 1;
    }
    /* No more data in buffer, end transmission */
    I2C_LLD_MasterEndTransfer(instance, master);
}

/*******************************************************************************
 * @brief      This function shall be the master transmit hander.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi     
 *******************************************************************************/
static void I2C_LLD_MasterTransmitHandler(uint8_t instance)
{
    const i2c_master_state_t * master;
    I2C_Type const *baseAddr;

    baseAddr = g_i2cBase[instance];
    master = g_i2cMasterStatePtr;                     /* Master Mode */

    if (I2C_Get_TCF(baseAddr) == 1u)                         /* Last byte transmitted ? */
    {
       if ((master->txSize == 0u) && ((master->sendData) == 1u))
       {
           I2C_LLD_MasterLastByteTransmitted(instance);
       }
       else
       {
           /* Receive acknowledge */
        if (I2C_Get_ReceivedACK(baseAddr) == 1u)
           {
               I2C_LLD_MasterReceiveAckEvent(instance);
           }
           else
           {
               I2C_LLD_MasterGenerateStopEvent(instance);
           }
       }
    }
}

/*******************************************************************************
 * @brief      This function shall be hander the master receive.
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_MasterReceiveHandler(uint8_t instance)
{
    /* Master is in receive mode */
    i2c_master_state_t * master;
    I2C_Type *baseAddr;

    baseAddr = g_i2cBase[instance];
    master = g_i2cMasterStatePtr;

    /* If last byte to be read */
    if (master->rxSize == 1U)
    {
        I2C_LLD_MasterReceiveDataEvent(instance, master);
        if (master->sendStop == 1u)
        {
            /* Generate Stop Signal */
            I2C_LLD_GenerateStopSignal(instance);
            master->i2cIdle = 1;
            I2C_Set_ModuleTransmitMode(baseAddr, (uint8_t)I2C_MASTER);
        }
        else
        {
            /* Generate repeated start */
            I2C_Set_RepeatStart(baseAddr, 1);
            master->stopGenerated = 0;
            master->i2cIdle = 1;
        }
    }
    else
    {
        /* If second last byte to be read */
        if (master->rxSize == 2U)
        {
            /* Set noAck = 1 */
            I2C_Set_DataAcknowledge(baseAddr, 0);
        }
    }
    /* Read data from IBDR and store */
    I2C_LLD_MasterReceiveDataEvent(instance, master);
}

/*******************************************************************************
 * @brief      This function shall be the IRQ master handler.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_MasterIRQHandler(uint8_t instance)
{
    I2C_Type const *baseAddr;

    baseAddr = g_i2cBase[instance];

    /* Check the mode - receive or transmit */
    if (I2C_Get_ModuleTransmitMode(baseAddr) == 1u)
    {
        I2C_LLD_MasterTransmitHandler(instance);
    }
    else
    {
        I2C_LLD_MasterReceiveHandler(instance);
    }
}

/*******************************************************************************
 * @brief      This function shall be handle master arbitration lost.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_MasterArbitrationLostHandler(uint8_t instance)
{
    i2c_master_state_t * master;

    I2C_Type *baseAddr;

    baseAddr = g_i2cBase[instance];

    /* If the module is slave */
    master = g_i2cMasterStatePtr ;

    /* Clear the master arbitration lost flag */
    I2C_Clear_MasterArbitrationLostFlag(baseAddr);

    /* End transfer */
    I2C_LLD_MasterEndTransfer(instance, master);

    master->stopGenerated = 1;
}

/*******************************************************************************
 * @brief      This function shall be handle master to slave.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_MasterToSlaveHandler(uint8_t instance)
{
    I2C_Type  const *baseAddr;
    baseAddr = g_i2cBase[instance];

    if (I2C_Get_MasterArbitrationLostEvent(baseAddr) == 1u)
	{
		I2C_LLD_MasterArbitrationLostHandler(instance);
	}
    if (I2C_Get_BusBusyEvent(baseAddr) == 0u)
    {
    	I2C_LLD_BusBusyEventHandler(instance);
    }
}

/*******************************************************************************
 * @brief      This function shall be the slave stop event.
 * @param[in]  I2C_Type *baseAddr, i2c_slave_state_t *slave
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_SlaveStopEvent(I2C_Type *baseAddr, i2c_slave_state_t *slave)
{
    I2C_LLD_SlaveEndTransfer(baseAddr, slave);
}

/*******************************************************************************
 * @brief      This function shall be the slave send data event.
 * @param[in]  I2C_Type *baseAddr, i2c_slave_state_t *slave
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_SlaveSendDataEvent(I2C_Type *baseAddr, i2c_slave_state_t *slave)
{
    if (slave->txSize > 0U)
    {
        /* Send data to master*/
        I2C_Set_DataIBDR(baseAddr, slave->txBuff[0]);
        slave->txBuff++;
        slave->txSize--;
        if (slave->txSize == 0u)
        {
        	slave->txBuff =  NULL;
        }
    }
    else
    {
    }
}

/*******************************************************************************
 * @brief      This function shall be the address as slave envent.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_AddressAsSlaveEvent(I2C_Type *baseAddr)
{
    /* Check if slave is transmitting or receiving - check SRW bit */
    if (I2C_Get_SlaveTransmitMode(baseAddr) == 1u)
    {
        /* Set TX Mode */
        I2C_Set_ModuleTransmitMode(baseAddr, 1);
        /* Write data to IBDR to clear the IAAS bit! */
    }
    else
    {
        /* Set RX Mode */
        I2C_Set_ModuleTransmitMode(baseAddr, 0);
        /* Dummy reading */
        (void) I2C_Get_DataIBDR(baseAddr);
    }
}

/*******************************************************************************
 * @brief      This function shall be slave receive data event.
 * @param[in]  uint8_t instance, i2c_slave_state_t *slave
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_SlaveReceiveDataEvent(uint8_t instance, i2c_slave_state_t *slave)
{
    I2C_Type *baseAddr;
    baseAddr = g_i2cBase[instance];

    if (slave->rxSize > 0U)
    {
        /* Read data from IBDR and store it */
        slave->rxBuff[0U] = I2C_Get_DataIBDR(baseAddr);
        slave->rxBuff++;
        slave->rxSize--;
    }
    if (slave->rxSize == 0U)
    {
        /* Slave end transfer */
        I2C_LLD_SlaveStopEvent(baseAddr, slave);
        /* Dummy read from IBDR */
        (void) I2C_Get_DataIBDR(baseAddr);
    }
}

/*******************************************************************************
 * @brief      This function shall be the slave end transfer.
 * @param[in]  I2C_Type *baseAddr, i2c_slave_state_t *slave
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_SlaveEndTransfer(I2C_Type *baseAddr, i2c_slave_state_t *slave)
{
    /* Deactivate events */
    I2C_Set_BusIRQEnable(baseAddr, 0);
    /* Disable I2C slave */
    I2C_Set_ModuleEnable(baseAddr, 0);

    slave->isTransferInProgress = 0;
    slave->rxBuff = NULL;
    slave->rxSize = 0U;
    slave->txBuff = NULL;
    slave->txSize = 0U;
}

/*******************************************************************************
 * @brief      This function shall be the slave transmit data event.
 * @param[in]  uint32_t instance
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_SlaveTransmitDataEvent(uint32_t instance)
{
    i2c_slave_state_t * slave;
    I2C_Type *baseAddr;

    baseAddr = g_i2cBase[instance];
    /* If the module is slave */
    slave = g_i2cSlaveStatePtr;

      /* Check the acknowledge from master */
    if (I2C_Get_ReceivedACK(baseAddr) == 1u)
    {
        /* Transmit next byte to master */
        I2C_LLD_SlaveSendDataEvent(baseAddr, slave);
    }
    else
    {
        if (slave->txSize == 0u)
        {
            I2C_LLD_SlaveStopEvent(baseAddr, slave);
        }
        /* Switch to RX mode and dummy read */
        I2C_Set_ModuleTransmitMode(baseAddr, 0);
        (void) I2C_Get_DataIBDR(baseAddr);
    }
}

/*******************************************************************************
 * @brief      This function shall be handle slave transfer.
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_SlaveTransferHandler(uint8_t instance)
{
    i2c_slave_state_t *slave;
    I2C_Type const *baseAddr;

    baseAddr = g_i2cBase[instance];
    slave = g_i2cSlaveStatePtr;

    /* Module is transmitting or receiving */
    if (I2C_Get_ModuleTransmitMode(baseAddr) == 1u)
    {
    	I2C_LLD_SlaveTransmitDataEvent(instance);
    }
    else
    {
        if (I2C_Get_TCF(baseAddr) == 1u)
        {
            /* Read data from IBDR and store it */
            I2C_LLD_SlaveReceiveDataEvent(instance, slave);
        }
    }
}

/*******************************************************************************
 * @brief      This function shall be the slave handle transfer event.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_SlaveHandleTransferEvents(uint8_t instance)
{
    i2c_slave_state_t * slave;
    I2C_Type *baseAddr;

    baseAddr = g_i2cBase[instance];
    slave = g_i2cSlaveStatePtr;                /* If the module is slave */

    /* Check if slave is addressed as slave */
    if (I2C_Get_AddressAsSlave(baseAddr) == 1u)
    {
        /* It was an address transfer */
        I2C_LLD_AddressAsSlaveEvent(baseAddr);
    }
    if (I2C_Get_BusBusyEvent(baseAddr) == 0u)
    {
        I2C_LLD_SlaveStopEvent(baseAddr, slave);       /** End  **/
    }
    else
    {
        I2C_LLD_SlaveTransferHandler(instance);
    }
}

/*******************************************************************************
 * @brief      This function shall be handle bus busy event.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_BusBusyEventHandler(uint8_t instance)
{
    i2c_master_state_t * master;

    /* If the module is slave */
    master = g_i2cMasterStatePtr;

    I2C_LLD_MasterEndTransfer(instance, master);
}

/*******************************************************************************
 * @brief      This function shall be handle slave arbitration lost
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_SlaveArbitrationLostHandler(uint8_t instance)
{
    I2C_Type *baseAddr;

    baseAddr = g_i2cBase[instance];

    /* Clear the arbitration lost flag */
    I2C_Clear_MasterArbitrationLostFlag(baseAddr);

    /* Check address as slave flag */
    if (I2C_Get_AddressAsSlave(baseAddr) == 1u)
    {
        /* It was an address transfer */
        I2C_LLD_AddressAsSlaveEvent(baseAddr);
    }
}

/*******************************************************************************
 * @brief      This function shall be the slave handle IRQ.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_SlaveIRQHandler(uint8_t instance)
{
    i2c_master_state_t const *master;
    I2C_Type const *baseAddr;
    baseAddr = g_i2cBase[instance];

    /* If the module is slave */
    master = g_i2cMasterStatePtr;
    if (master != NULL)
    {
        /* Master arbitration lost event */
        I2C_LLD_MasterToSlaveHandler(instance);
    }
    else
    {
         /* Check arbitration lost event */
        if (I2C_Get_MasterArbitrationLostEvent(baseAddr) == 1u)
        {
            I2C_LLD_SlaveArbitrationLostHandler(instance);
        }
        else
        {
            /* No arbitration lost event */
            I2C_LLD_SlaveHandleTransferEvents(instance);
        }
    }
     /* No arbitration lost event */
}

/*******************************************************************************
 * @brief      This function shall be master receive data event.
 * @param[in]  uint8_t instance, i2c_master_state_t *master
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_LLD_MasterReceiveDataEvent(uint8_t instance, i2c_master_state_t *master)
{
    I2C_Type const *baseAddr;

    baseAddr = g_i2cBase[instance];

    if (master->rxSize > 0U)
    {
        /* Receive data from slave and store it */
        master->rxBuff[0U] = I2C_Get_DataIBDR(baseAddr);
        master->rxBuff++;
        master->rxSize--;
    }
}

/*******************************************************************************
 * @brief      This function shall be IRQ enable.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
static void I2C_Init_IrqIsrEnable(uint8_t instance)
{
	switch (instance)
	{
	case 0:
		INTC_InstallINTCInterruptHandler(I2C_LLD_IRQHandler0, (uint16_t)I2C0_SR_IRQn, (uint8_t)PSR_PRI_8);
		break;
	default:
		break;
	}
}

/*******************************************************************************
 * @brief      This function shall be the slave receive
 * @param[in]  uint32_t instance, uint8_t *rxBuff, uint32_t rxSize
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint8_t I2C_LLD_Slave_Receive(uint32_t instance, uint8_t *rxBuff, uint32_t rxSize)
{
    i2c_slave_state_t *slave;
    I2C_Type *baseAddr;

    baseAddr = g_i2cBase[instance];

    slave = g_i2cSlaveStatePtr;
    /** Check if driver is busy **/
    while (slave->isTransferInProgress == 1u)
    {}

    slave->rxBuff = rxBuff;
    slave->rxSize = rxSize;
    slave->isTransferInProgress = 1;

    /** Enable I2C Module **/
    I2C_Set_ModuleEnable(baseAddr, 1);

    /** Slave Mode **/
    I2C_Set_ModuleModSelect(baseAddr, I2C_SLAVE);

    /**  Enable/Disable Acknowledge **/
    I2C_Set_DataAcknowledge(baseAddr, slave->dataAck);

    /** Clear bus interrupt flag **/
    I2C_Clear_BusInterruptFlag(baseAddr, 1);

    /** Interrupt enabled **/
    I2C_Set_BusIRQEnable(baseAddr, 1);

    /** Enable bus idle interrupts **/
    I2C_Set_BusIdleIRQ(baseAddr, 1);

    return 0;
}

/*******************************************************************************
 * @brief      This function shall be the master receive.
 * @param[in]  uint8_t instance, uint8_t *rxBuff, uint32_t rxSize, uint8_t sendStop
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint8_t I2C_LLD_Master_Receive(uint8_t instance, uint8_t *rxBuff, uint32_t rxSize, uint8_t sendStop)
{
    i2c_master_state_t *master;
    I2C_Type *baseAddr;
    static uint8_t flag = 1;
    baseAddr = g_i2cBase[instance];
    master = g_i2cMasterStatePtr;
    while (master->i2cIdle == 0u)
    {}
    while (flag == 1u)
    {
        flag = I2C_Get_BusBusyEvent(baseAddr);
    }

    /* Copy parameters to driver state structure */
    master->rxSize = rxSize;
    master->i2cIdle = 0;
    master->rxBuff = rxBuff;
    master->sendData = 0;
    master->sendStop = sendStop;

    /* Acknowledge enabled */
    I2C_Set_DataAcknowledge(baseAddr, 1);

    /* Enable interrupts */
    I2C_Set_BusIRQEnable(baseAddr, 1);

    /* Activate interrupts for bus idle*/
    I2C_Set_BusIdleIRQ(baseAddr, 1);

    (void)I2C_LLD_MasterTransmitAddress(instance);

    return 0;
}

/*******************************************************************************
 * @brief      This function shall be the master INIT.
 * @param[in]  uint8_t instance, i2c_master_state_t * master
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void I2C_LLD_Master_Init(uint8_t instance, i2c_master_state_t * master)
{
    I2C_Type *baseAddr;
    baseAddr = g_i2cBase[instance];

    g_i2cMasterStatePtr = master;

    /** Initialize driver status structure **/
    master->rxBuff = NULL;
    master->rxSize = 0U;
    master->txBuff = NULL;
    master->txSize = 0U;
    master->i2cIdle = 1;
    master->stopGenerated = 1;

    I2C_Init_IrqIsrEnable(instance);

    I2C_ResetModuleState(baseAddr);             /** Reset I2C module **/

    baseAddr->IBFD = I2C_BAUDRATE_100K;      /** 100k:0x65   200k: 0x5D **/

    baseAddr->IBAD = 0;

    I2C_Set_ModuleEnable(baseAddr, 1);

    I2C_Set_BusIRQEnable(baseAddr, 1);       /** enable interrupt **/
}

/*******************************************************************************
 * @brief      This function shall be the slave INIT
 * @param[in]  uint8_t instance, i2c_slave_state_t * slave
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void I2C_LLD_Slave_Init(uint8_t instance, i2c_slave_state_t * slave)
{
    I2C_Type *baseAddr;
    baseAddr = g_i2cBase[instance];

    g_i2cSlaveStatePtr = slave;

    /* Initialize driver status structure */
    slave->txBuff = NULL;
    slave->rxBuff = NULL;
    slave->txSize = 0U;
    slave->rxSize = 0U;
    slave->dataAck = 1;
    slave->isTransferInProgress = 0;
    slave->blocking = 0;

    I2C_Init_IrqIsrEnable(instance);

    I2C_ResetModuleState(baseAddr);

    baseAddr->IBFD = 0x65;

    I2C_Set_SlaveAddress(baseAddr, slave->slaveAddress);     /** slave address **/

    I2C_Set_ModuleModSelect(baseAddr, I2C_SLAVE);                     /** I2C IN slave mode **/

    I2C_Set_ModuleEnable(baseAddr, 0);                        /** enable i2 module **/

    I2C_Set_BusIRQEnable(baseAddr, 1);                        /** enable interrupt **/

    I2C_Set_ModuleTransmitMode(baseAddr, 0);
}

/*******************************************************************************
 * @brief      This function shall be the slave transmit
 * @param[in]  uint8_t instance, const uint8_t * txBuff, uint32_t txSize
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint8_t I2C_LLD_Slave_Transmit(uint8_t instance, const uint8_t * txBuff, uint32_t txSize)
{
    i2c_slave_state_t * slave;
    I2C_Type *baseAddr;

    baseAddr = g_i2cBase[instance];

    slave = g_i2cSlaveStatePtr;
    /* Check if slave is busy */

    while (slave->isTransferInProgress == 1u)
    {}
    slave->txBuff = txBuff;
    slave->txSize = txSize;

    /* Interrupts enabled*/
    I2C_Set_BusIRQEnable(baseAddr, 1);

    /* Enable I2C Module*/
    I2C_Set_ModuleEnable(baseAddr, 1);

    /* Enable bus idle interrupts */
    I2C_Set_BusIdleIRQ(baseAddr, 1);

    /* Clear bus interrupt flag*/
    I2C_Clear_BusInterruptFlag(baseAddr, 1);

    slave->isTransferInProgress = 1;

    return 0;
}

/*******************************************************************************
 * @brief      This function shall be the master transmit
 * @param[in]  uint8_t instance, uint8_t *txBuff, uint32_t txSize, uint8_t sendStop
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint8_t I2C_LLD_Master_Transmit(uint8_t instance, const uint8_t *txBuff, uint32_t txSize, uint8_t sendStop)
{
    i2c_master_state_t *master;

    I2C_Type *baseAddr;

    baseAddr = g_i2cBase[instance];

    master = g_i2cMasterStatePtr;
    /* Check if driver is busy */
    if (master->i2cIdle == 0u)
    {
         return 1;
    }

    if (master->stopGenerated == 1u)
    {
        /* Check if bus is busy */
        if (I2C_Get_BusBusyEvent(baseAddr) == 1u)
        {
            return 1;
        }
    }

    /* Copy parameters to drive state structure */
    master->txBuff = txBuff;
    master->txSize = txSize;
    master->i2cIdle = 0;
    master->sendData = 1;
    master->sendStop = sendStop;

    /* Activate interrupts for DMA transfer*/
    I2C_Set_BusIRQEnable(baseAddr, 1);
    /* Activate interrupts for bus idle */
    I2C_Set_BusIdleIRQ(baseAddr, 1);

    (void)I2C_LLD_MasterTransmitAddress(instance);

    return 0;
}

/*******************************************************************************
 * @brief      This function shall be the IRQ handle.
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     NOne
 * @notapi
 *******************************************************************************/
static void I2C_LLD_IRQHandler(uint8_t instance)
{
    I2C_Type *baseAddr;
    baseAddr = g_i2cBase[instance];

    /* Clear I - Bus Interrupt */
    I2C_Clear_BusInterruptFlag(baseAddr, 1);

    if (I2C_Get_ModuleModSelect(baseAddr) == (uint8_t)I2C_MASTER)
    {
        I2C_LLD_MasterIRQHandler(instance);
    }
    else
    {
        I2C_LLD_SlaveIRQHandler(instance);
    }
}

/*******************************************************************************
 * @brief      This function shall be the polling slave INIT
 * @param[in]  uint8_t instance, uint8_t address
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void I2C_LLD_Polling_Slave_Init(uint8_t instance, uint8_t address)
{
    I2C_Type *baseAddr;
    baseAddr = g_i2cBase[instance];
    baseAddr->IBFD = I2C_BAUDRATE_100K;                 /** Set I2C Baud Rate **/
    I2C_Set_ModuleModSelect(baseAddr, I2C_SLAVE);          /** Set Slave mode **/
    I2C_Set_SlaveAddress(baseAddr, address);                /** Set Slave address **/
    I2C_Set_ModuleEnable(baseAddr, (uint8_t)I2C_ENABLE);          /** Enable I2C module **/
    I2C_Set_BusIRQEnable(baseAddr, (uint8_t)I2C_ENABLE);          /** Enable I2C Interrupt **/
    I2C_Set_ModuleTransmitMode(baseAddr, (uint8_t)I2C_RECEIVE);   /** Set Receive mode **/
}

/*******************************************************************************
 * @brief      This function shall be the polling master INIT
 * @param[in]  uint8_t instance
 * @param[out] None
 * @retval     None
 * @notapi
*******************************************************************************/
void I2C_LLD_Polling_Master_Init(uint8_t instance)
{
    I2C_Type *baseAddr;

    baseAddr = g_i2cBase[instance];

    baseAddr->IBFD = I2C_BAUDRATE_100K;

    baseAddr->IBAD = 0;

    I2C_Set_ModuleEnable(baseAddr, 1);                  /** Enable I2C module **/
}

/*******************************************************************************
 * @brief      This function shall be the polling slave transmit
 * @param[in]  uint8_t instance, uint8_t *txbuff, uint8_t txcount
 * @param[out] txbuff
 * @retval     None
 * @notapi
 *******************************************************************************/
void I2C_Lld_Polling_Slave_Transmit(uint8_t instance, const uint8_t *txbuff, uint8_t txcount)
{
    I2C_Type *baseAddr;
    uint8_t i = 0;
    uint8_t txcount_temp = txcount + 1u;
    uint8_t const *ptemp = txbuff;
    uint8_t temp = 0u;
    uint32_t timeout_count;
    baseAddr = g_i2cBase[instance];

    temp = I2C_Get_ModuleTransmitMode(baseAddr);
    if ((I2C_Get_BusInterruptFlag(baseAddr) == 1u) || (temp == 1u))
    {
        if (I2C_Get_MasterArbitrationLostEvent(baseAddr) == 0u)
        {
            I2C_Clear_MasterArbitrationLostFlag(baseAddr);
            temp = I2C_Get_SlaveTransmitMode(baseAddr);
            if ((I2C_Get_AddressAsSlave(baseAddr) == 1u) && (temp == 1u))
            {
                /** master read, slave transmit **/
                I2C_Set_ModuleTransmitMode(baseAddr, 1);
                for (i = 0; i < txcount_temp; i++)
                {
                    I2C_Clear_BusInterruptFlag(baseAddr, 1);
                    baseAddr->IBDR = ptemp[0];
                    timeout_count = I2C_TIMEOUT;
                    while (I2C_Get_BusInterruptFlag(baseAddr) == 0u)
                    {
                    	timeout_count -= 1;
                    	if(timeout_count == 0u)
                    	{
                    	    I2C_LLD_Polling_Stop(baseAddr); 
                    		return;
                    	}
                    }
                    I2C_Clear_BusInterruptFlag(baseAddr, 1);
                    ptemp++;
                }
            }
        }
    }
}

/*******************************************************************************
 * @brief      This function shall be polling send.
 * @param[in]  uint8_t data, I2C_Type *baseAddr
 * @param[out] None
 * @retval     0 or 1
 * @notapi
 *******************************************************************************/
static uint8_t I2C_LLD_Polling_Send(uint8_t data, I2C_Type *baseAddr)
{
	uint32_t timeout_count;
	
    I2C_Set_DataIBDR(baseAddr, data);
    timeout_count = I2C_TIMEOUT;
	while (I2C_Get_BusInterruptFlag(baseAddr) == 0u)
	{
    	timeout_count -= 1;
    	if(timeout_count == 0u)
    	{
    	    I2C_LLD_Polling_Stop(baseAddr); 
    		return 1;
    	}
	}
    
    I2C_Clear_BusInterruptFlag(baseAddr, 1);

    if (I2C_Get_ReceivedACK(baseAddr) == 0u)
    {
    	return 1;
    }
    else
    {
    	return 0;
    }
}

/*******************************************************************************
 * @brief      This function shall be polling stop.
 * @param[in]  I2C_Type *baseAddr
 * @param[out] None
 * @retval     None
 * @notapi
*******************************************************************************/
static void I2C_LLD_Polling_Stop(I2C_Type *baseAddr)
{
	uint32_t timeout_count;
    I2C_Set_ModuleModSelect(baseAddr, I2C_SLAVE);

    timeout_count = I2C_TIMEOUT;
	do
	{
    	timeout_count -= 1;
    	if(timeout_count == 0u)
    	{
    	    I2C_LLD_Polling_Stop(baseAddr); 
    		return;
    	}
		I2C_Clear_BusInterruptFlag(baseAddr, 1);
	}
	while (I2C_Get_BusInterruptFlag(baseAddr) == 1u);
	
	/** delay for waiting for IBIF&IBAL bits cleared, value 200 is guessed, 100 will fault. **/
	delay(200);
	
	timeout_count = I2C_TIMEOUT;
	do
	{
    	timeout_count -= 1;
    	if(timeout_count == 0u)
    	{
    	    I2C_LLD_Polling_Stop(baseAddr); 
    		return;
    	}
		I2C_Clear_MasterArbitrationLostFlag(baseAddr);
	}
	while (1u == I2C_Get_MasterArbitrationLostEvent(baseAddr));
}

/*******************************************************************************
 * @brief      This function shall be the polling master receive
 * @param[in]  uint8_t instance, int8_t rx_count , uint8_t *rxBuff
 * @param[out] *rxBuff
 * @retval     uint8_t
 * @notapi
*******************************************************************************/
uint8_t I2C_LLD_Polling_Master_Receive(uint8_t instance, uint8_t rx_count , uint8_t *rxBuff, uint8_t Slave_Address)
{
    I2C_Type *baseAddr;
    uint8_t count_temp = rx_count;
    uint8_t *ptemp = rxBuff;
    uint32_t timeout_count;
    
    baseAddr = g_i2cBase[instance];
    timeout_count = I2C_TIMEOUT;
    /** Get the IIC bus busy **/
    while (I2C_Get_BusBusyEvent(baseAddr) == 1u)
    {
    	timeout_count -= 1;
    	if(timeout_count == 0u)
    	{
    	    I2C_LLD_Polling_Stop(baseAddr); 
    		return 1;
    	}
    }

    /** Configure the IIC device **/
    I2C_Set_ModuleModSelect(baseAddr, I2C_MASTER);
    I2C_Set_ModuleTransmitMode(baseAddr, 1);

    /** Transmit the slave address **/
    if (I2C_LLD_Polling_Send(Slave_Address, baseAddr) == 1u)
    {
        I2C_LLD_Polling_Stop(baseAddr);
        return 1;
    }

    /** If receive only one byte, No ACK **/
    if (count_temp == 1u)
    {
         I2C_Set_DataAcknowledge(baseAddr, 0);
    }
    else
    {
         I2C_Set_DataAcknowledge(baseAddr, 1);
    }

    I2C_Set_ModuleTransmitMode(baseAddr, 0);
    (void)I2C_Get_DataIBDR(baseAddr);                 /** must read IBDR register before data transfer **/

    timeout_count = I2C_TIMEOUT;
    while (I2C_Get_BusInterruptFlag(baseAddr) == 0u)
    {
    	timeout_count -= 1;
    	if(timeout_count == 0u)
    	{
    	    I2C_LLD_Polling_Stop(baseAddr); 
    		return 1;
    	}
    }
    
    timeout_count = I2C_TIMEOUT;
    while (0u == I2C_Get_TCF(baseAddr))
    {
    	timeout_count -= 1;
    	if(timeout_count == 0u)
    	{
    	    I2C_LLD_Polling_Stop(baseAddr); 
    		return 1;
    	}
    }
    if (count_temp != 1u)
    {
    	timeout_count = I2C_TIMEOUT;
        while (I2C_Get_ReceivedACK(baseAddr) == 0u)
        {
        	timeout_count -= 1;
        	if(timeout_count == 0u)
        	{
        	    I2C_LLD_Polling_Stop(baseAddr); 
        		return 1;
        	}
        }
    }

    I2C_Clear_BusInterruptFlag(baseAddr, 1);
    count_temp = count_temp - 1u;

    if (count_temp == 0u)
    {
        ptemp[0] = I2C_Get_DataIBDR(baseAddr);
        I2C_LLD_Polling_Stop(baseAddr);
        return  0;
    }

    while (count_temp > 0u)
    {                  /** this set can effect in last byte.so the last byte should send STOP rather than send ACK, **/
    	if (count_temp == 1u)
    	{
    		I2C_Set_DataAcknowledge(baseAddr, 0u);
    	}

    	if (1u == (I2C_Get_TCF(baseAddr)))
    	{
    		ptemp[0] = I2C_Get_DataIBDR(baseAddr);
    		ptemp++;
    	}

    	timeout_count = I2C_TIMEOUT;
    	while (I2C_Get_BusInterruptFlag(baseAddr) == 0u)
        {
    		timeout_count -= 1;
    		if(timeout_count == 0u)
    		{
    		    I2C_LLD_Polling_Stop(baseAddr); 
    			return 1;
    		}
        }
         
    	timeout_count = I2C_TIMEOUT;
    	while (0u == (I2C_Get_TCF(baseAddr)))
    	{
    		timeout_count -= 1;
    		if(timeout_count == 0u)
    		{
    		    I2C_LLD_Polling_Stop(baseAddr); 
    			return 1;
    		}
    	}
    	if (count_temp != 1u)
    	{
        	timeout_count = I2C_TIMEOUT;
        	while (I2C_Get_ReceivedACK(baseAddr) == 0u)
        	{
        		timeout_count -= 1;
        		if(timeout_count == 0u)
        		{
        		    I2C_LLD_Polling_Stop(baseAddr); 
        			return 1;
        		}
        	}
    	}
        I2C_Clear_BusInterruptFlag(baseAddr, 1);
        count_temp--;
    }
    ptemp[0] = I2C_Get_DataIBDR(baseAddr);
    I2C_LLD_Polling_Stop(baseAddr);
     
    return 0;
}

/*******************************************************************************
 * @brief      This function shall be the polling salve receive
 * @param[in]  uint8_t instance, uint8_t *rxbuff, uint8_t rxcount
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void I2C_LLD_Poliing_Slave_Receive(uint8_t instance, uint8_t *rxbuff, uint8_t rxcount)
{
    
    uint8_t i = 0u;
    uint8_t temp = 0u;
    I2C_Type *baseAddr;
    uint8_t rxcount_temp = rxcount + 1u;
    uint8_t *ptemp = rxbuff;
    uint32_t timeout_count;

    baseAddr = g_i2cBase[instance];
    temp = I2C_Get_ModuleTransmitMode(baseAddr);
    if ((I2C_Get_BusInterruptFlag(baseAddr) == 1u) || (temp == 1u))
    {
    	timeout_count = I2C_TIMEOUT;
        while (I2C_Get_MasterArbitrationLostEvent(baseAddr) == 1u)
        {
        	timeout_count -= 1;
        	if(timeout_count == 0u)
        	{
        	    I2C_LLD_Polling_Stop(baseAddr); 
        		return;
        	}
        }
        I2C_Clear_BusInterruptFlag(baseAddr, 1);
        temp = I2C_Get_SlaveTransmitMode(baseAddr);
        if ((I2C_Get_AddressAsSlave(baseAddr) == 1u) && (temp == 0u))
        {
           /** master write slave read **/
            I2C_Set_ModuleTransmitMode(baseAddr, 0);
            
            timeout_count = I2C_TIMEOUT;
            while (I2C_Get_ModuleTransmitMode(baseAddr) == 1u)
            {
            	timeout_count -= 1;
            	if(timeout_count == 0u)
            	{
            	    I2C_LLD_Polling_Stop(baseAddr); 
            		return;
            	}
            }
            for (i = 0; i < rxcount_temp; i++)
            {
                I2C_Clear_BusInterruptFlag(baseAddr, 1);
                if (1u == (I2C_Get_TCF(baseAddr)))
                {
                    if (i == 0u)
                    {
                        (void)I2C_Get_DataIBDR(baseAddr);
                    }
                    else
                    {
                        ptemp[0] = baseAddr->IBDR;
                        ptemp++;
                    }
                }
                
                timeout_count = I2C_TIMEOUT;
                while (I2C_Get_BusInterruptFlag(baseAddr) == 0u)
                {
                	timeout_count -= 1;
                	if(timeout_count == 0u)
                	{
                	    I2C_LLD_Polling_Stop(baseAddr); 
                		return;
                	}
                }
            }
        }
    }
}

/*******************************************************************************
  * @brief      This function shall be the polling master transmit
  * @param[in]  uint8_t instance, uint8_t tx_count, uint8_t *txbuff
  * @param[out] None
  * @retval     None
  * @notapi
  *******************************************************************************/
uint8_t I2C_LLD_Pollling_Master_Transmit(uint8_t instance,  uint8_t tx_count, const uint8_t * txbuff, uint8_t Slave_Address)
{
    I2C_Type *baseAddr;
    uint8_t tx_count_temp = tx_count;
    uint8_t const *ptemp = txbuff;
    uint8_t temp = 0;
    uint32_t timeout_count;
    baseAddr = g_i2cBase[instance];
    
    timeout_count = I2C_TIMEOUT;
    while (I2C_Get_BusBusyEvent(baseAddr) == 1u)
    {
    	timeout_count -= 1;
    	if(timeout_count == 0u)
    	{
    	    I2C_LLD_Polling_Stop(baseAddr); 
    		return 1;
    	}
    }
    
    I2C_Set_ModuleModSelect(baseAddr, I2C_MASTER);
    I2C_Set_ModuleTransmitMode(baseAddr, 1);

    if (I2C_LLD_Polling_Send(Slave_Address, baseAddr) == 1u)
    {
       I2C_LLD_Polling_Stop(baseAddr);
       return 1;
    }

    for (; ;)
    {
        temp = I2C_Get_ReceivedACK(baseAddr);
        if ((tx_count_temp == 0u) || (0u == temp))
        {
            I2C_LLD_Polling_Stop(baseAddr);                 /** Stop IIC bus **/
            return 0;
        }
        else
        {
            I2C_Set_DataIBDR(baseAddr, *ptemp);
            
            timeout_count = I2C_TIMEOUT;
            while (I2C_Get_BusInterruptFlag(baseAddr) == 0u)
            {
            	timeout_count -= 1;
            	if(timeout_count == 0u)
            	{
            	    I2C_LLD_Polling_Stop(baseAddr); 
            		return 1;
            	}
            }
            
            timeout_count = I2C_TIMEOUT;
            while (0u == I2C_Get_TCF(baseAddr))
            {
            	timeout_count -= 1;
            	if(timeout_count == 0u)
            	{
            	    I2C_LLD_Polling_Stop(baseAddr); 
            		return 1;
            	}
            }

            timeout_count = I2C_TIMEOUT;
			/** check slave ACK **/
            while (0u == I2C_Get_ReceivedACK(baseAddr))
            {
            	timeout_count -= 1;
            	if(timeout_count == 0u)
            	{
            	    I2C_LLD_Polling_Stop(baseAddr); 
            		return 1;
            	}
            }
            
            I2C_Clear_BusInterruptFlag(baseAddr, 1);
            tx_count_temp--;
            ptemp++;
        }
    }
}

/*******************************************************************************
 * @brief      This function shall be the polling master transmit
 * @param[in]  uint8_t instance, uint8_t tx_count, uint8_t *txbuff
 * @param[out] None
 * @retval     None
 * @notapi
*******************************************************************************/
void I2C_LLD_Pollling_Master_Transmit_Dma(uint8_t instance, uint8_t tx_count,  const uint8_t *txbuff)
{
   
    I2C_Type *baseAddr;
    int32_t temp = 0;
    uint32_t timeout_count;
 
    baseAddr = g_i2cBase[instance]; 
    
    timeout_count = I2C_TIMEOUT;
    while (I2C_Get_BusBusyEvent(baseAddr) == 1u)
    {
    	timeout_count -= 1;
    	if(timeout_count == 0u)
    	{
    	    I2C_LLD_Polling_Stop(baseAddr); 
    		return;
    	}
    }
    I2C_Set_ModuleModSelect(baseAddr, I2C_MASTER);
    I2C_Set_ModuleTransmitMode(baseAddr, 1);
    DMAMUX_ChConfigEn(0, 32);                       /** DMA_MUX Source #32 : IIC_TX   **/
    
    I2C_Set_DMAEnable(baseAddr, 1);           /** Activate DMA **/
    EDMA.SERQR.R = 0;                        /** Enable EDMA channel 0 **/
    
    temp = (int32_t)tx_count;
    temp = - temp + 3;
    EdmaChannelSetup_C(0u, (vuint32_t)txbuff, (vuint32_t)&I2C_0.IBDR.R , 1u, 0u, 0u, 0u, 1, \
                        (uint16_t)tx_count + 1u, temp, 1, 0);
    EDMA.TCD[0u].START = 1u;                   /* initiate status flags */

    while (EDMA.TCD[0].DONE == 0u)
    {}
    delayms(10);
    EDMA.CIRQR.B.CINT = 1;
    I2C_LLD_Polling_Stop(baseAddr); 
}

/*******************************************************************************
 * @brief      This function shall be the polling receive transmit
 * @param[in]  uint8_t instance, uint8_t rx_count, const uint8_t *rxBuff, 
 *             uint8_t Slaver_Address
 * @param[out] None
 * @retval     None
 * @notapi
*******************************************************************************/
void I2c_LLD_Polling_Master_Receive_Dma(uint8_t instance, uint8_t rx_count, const uint8_t *rxBuff, uint8_t Slaver_Address)
{
     I2C_Type *baseAddr;
     uint32_t timeout_count;
     
     baseAddr = g_i2cBase[instance];

     I2C_ResetModuleState(baseAddr);

     I2C_LLD_Polling_Master_Init(instance);

     /** step1: Start bus **/
     timeout_count = I2C_TIMEOUT;
     while (I2C_Get_BusBusyEvent(baseAddr) == 1u)
     {
         timeout_count -= 1;
         if(timeout_count == 0u)
         {
     	     I2C_LLD_Polling_Stop(baseAddr); 
     		 return;
     	 }
     }
     I2C_Set_ModuleModSelect(baseAddr, I2C_MASTER);
     I2C_Set_ModuleTransmitMode(baseAddr, 1);

     DMAMUX_ChConfigEn(0, 31);                       /** DMA_MUX Source #31 : IIC_RX   **/

     if (I2C_LLD_Polling_Send(Slaver_Address, baseAddr) == 1u)
     {
          I2C_LLD_Polling_Stop(baseAddr);
          return;
     }
     I2C_Set_DataAcknowledge(baseAddr, 1);
     I2C_Set_ModuleTransmitMode(baseAddr, 0);
     (void)I2C_Get_DataIBDR(baseAddr);                    /** Dummy read from IBDR **/
     I2C_Set_DMAEnable(baseAddr, 1);                  /** Activate DMA **/

     EdmaChannelSetup_C(0u, (vuint32_t)&I2C_0.IBDR.R, (vuint32_t)rxBuff, 0u, 0u, 0u, 0, 1, (uint16_t)rx_count + 1u, 0, 0, 0);
     EDMA.SERQR.R = 0;                      /** Enable EDMA channel 0 **/

     EDMA.TCD[0].START = 1;                 /** initiate status flags **/

     while (EDMA.TCD[0].DONE == 0u)
     {
         if (EDMA.TCD[0].CITER == (rx_count))
         {
             EDMA.TCD[0].DOFF = 1;                         /* Do not increment destination addr */
         }
         if (EDMA.TCD[0].CITER == 1u)
         {
             I2C_Set_DataAcknowledge(baseAddr, 0);    /** The last byte no ACK **/
         }
     }
    EDMA.CIRQR.B.CINT = 1;
    I2C_LLD_Polling_Stop(baseAddr);
}
